package http

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"tanbao2/exception"
	"tanbao2/model"
	"tanbao2/mysql"
)

func InitRegisterMethods() {
	RegisterHandler("POST", "/api/searchUserInfo", searchUserInfo)
	RegisterHandler("POST", "/api/searchUserFinance", searchUserFinance)
	RegisterHandler("POST", "/api/batchUserByIds", batchUserByIds)
}

func searchUserInfo(ctx *gin.Context) {
	var request model.TB2RequestSearchUserInfo
	if err := ctx.BindJSON(&request); err != nil {
		ctx.JSON(http.StatusBadRequest, exception.NewParamException(fmt.Sprintln(err)))
		return
	}
	fmt.Printf("req:%+v\n", request)
	user, err := mysql.QueryUserInfo(request.ReqBody.UserName, request.ReqBody.Telephone)
	if err != nil {
		ctx.JSON(http.StatusBadRequest, exception.NewDBException(err.Error()))
		return
	}

	fmt.Printf("response:%+v\n", user)
	ctx.JSON(http.StatusOK, model.HttpResponse{CodeDesc: model.ResponseOK, Data: user})
}

func searchUserFinance(ctx *gin.Context) {
	var request model.TB2RequestSearchUserFinance
	if err := ctx.BindJSON(&request); err != nil {
		ctx.JSON(http.StatusBadRequest, exception.NewParamException(fmt.Sprintln(err)))
		return
	}
	fmt.Printf("req:%+v\n", request)
	info, err := mysql.QueryUserFinance(request.ReqBody.UserName, request.ReqBody.Telephone, request.ReqBody.WaterType, request.ReqBody.PageStart, request.ReqBody.PageSize)
	if err != nil {
		ctx.JSON(http.StatusBadRequest, exception.NewDBException(err.Error()))
		return
	}

	fmt.Printf("response:%+v\n", info)
	ctx.JSON(http.StatusOK, model.HttpResponse{CodeDesc: model.ResponseOK, Data: info})
}

func batchUserByIds(ctx *gin.Context) {
	var request model.TB2RequestBatchUserInfo
	if err := ctx.BindJSON(&request); err != nil {
		ctx.JSON(http.StatusBadRequest, exception.NewParamException(fmt.Sprintln(err)))
		return
	}
	user, err := mysql.BatchUserInfoByIds(request.Ids)
	if err != nil {
		ctx.JSON(http.StatusBadRequest, exception.NewDBException(err.Error()))
		return
	}
	fmt.Printf("response:%+v\n", user)
	ctx.JSON(http.StatusOK, model.HttpResponse{CodeDesc: model.ResponseOK, Data: user})
}
